# -*- coding:utf-8 -*-
import os
import tkMessageBox
from Tkinter import *
from ttk import *

import datetime

from utils.io_config import Config
from widgets.easy_editor import EasyEditor
from widgets.ui_widgets import FileEditor


class FileControl:
    def __init__(self, master, adb_manager):
        self.path = ""
        self.path_logs = ""
        self.path_shot = ""
        self.init_path()
        "构建编辑按钮"
        self.master = Labelframe(master, text="文件操作")

        self.adb = adb_manager

        self.init_ui()

    def init_ui(self):
        frm_1st = Frame(self.master)
        frm_1st.pack(side=TOP, expand=True, padx=4, anchor=W)
        self.btn_debug = Button(frm_1st, text="编辑debug", command=self.edit_debug_ini)
        self.btn_debug.pack(side=LEFT, padx=4, pady=2)
        self.btn_release = Button(frm_1st, text="编辑release", command=self.edit_release_ini)
        self.btn_release.pack(side=LEFT, padx=4, pady=2)
        self.btn_debug = Button(frm_1st, text="wdj_debug", command=self.edit_wdj_debug_ini)
        self.btn_debug.pack(side=LEFT, padx=4, pady=2)
        self.btn_release = Button(frm_1st, text="wdj_release", command=self.edit_wdj_release_ini)
        self.btn_release.pack(side=LEFT, padx=4, pady=2)
        frm_2st = Frame(self.master)
        frm_2st.pack(side=TOP, expand=True, padx=4, anchor=W)
        Button(frm_2st, text="logs目录", command=self.dir_logs).pack(side=LEFT, padx=4, pady=2)
        Button(frm_2st, text="拉取logs", command=self.pull_log).pack(side=LEFT, padx=4, pady=2)
        Button(frm_2st, text="清除logs", command=self.clean_log).pack(side=LEFT, padx=4, pady=2)

        frm_3st = Frame(self.master)
        frm_3st.pack(side=TOP, expand=True, padx=4, anchor=W)
        Button(frm_3st, text="截图目录", command=self.dir_screen_shot).pack(side=LEFT, padx=4, pady=2)
        Button(frm_3st, text="截图", command=self.screen_shot).pack(side=LEFT, padx=4, pady=2)

        self.master.pack(side=TOP, expand=False, padx=4, pady=4, anchor=W, fill="x")

    def edit_debug_ini(self):
        editor = EasyEditor(self.master, "debug.ini")
        self.btn_debug.wait_window(editor.root)  # 等待对话框结束
        # editor = FileEditor(self.master, "debug.ini")
        # self.btn_debug.wait_window(editor.top)  # 等待对话框结束

    def edit_release_ini(self):
        editor = EasyEditor(self.master, "release.ini")
        self.btn_release.wait_window(editor.root)  # 等待对话框结束
        # editor = FileEditor(self.root, "release.ini")
        # self.btn_release.wait_window(editor.top)  # 等待对话框结束

    def edit_wdj_debug_ini(self):
        editor = EasyEditor(self.master, "wdj_debug.ini")
        self.btn_debug.wait_window(editor.root)  # 等待对话框结束

    def edit_wdj_release_ini(self):
        editor = EasyEditor(self.master, "wdj_release.ini")
        self.btn_release.wait_window(editor.root)  # 等待对话框结束

    def send_action(self, action, is_batch=False, **kwargs):
        self.adb.receive_action(action, is_batch, **kwargs)

    def init_path(self):
        self.path = Config().get_workspace()  # 从配置文件中读取工作目录
        print "tool workspace:", self.path
        if not os.path.exists(self.path):
            os.makedirs(self.path)

        self.path_logs = self.path + "\\logs\\"
        if not os.path.exists(self.path_logs):
            os.makedirs(self.path_logs)

        self.path_shot = self.path + "\\screenshots\\"
        if not os.path.exists(self.path_shot):
            os.makedirs(self.path_shot)

    def pull_log(self):
        self.send_action("adb_pull_file", True, remote_path="/sdcard/crashsdk/logs/", local_path=self.path_logs)
        os.startfile(self.path_logs)

    def clean_log(self):
        if tkMessageBox.askokcancel('Adb Tool', '删除该设备所有的崩溃日志？'):
            self.send_action("adb_remove_file", False, remote_path="/sdcard/crashsdk/logs/", is_rf=True)

    def dir_logs(self):
        os.startfile(self.path_logs)

    def screen_shot(self):
        file_name = "%s.png" % datetime.datetime.now().strftime("%y%m%d%H%M%S")
        remote_path = "/sdcard/%s" % file_name
        self.send_action("adb_screencap", False, remote_path=remote_path)
        self.send_action("adb_pull_file", False, remote_path=remote_path, local_path=self.path_shot+file_name)
        self.send_action("adb_remove_file", False, remote_path=remote_path)
        os.startfile(self.path_shot+file_name)

    def dir_screen_shot(self):
        os.startfile(self.path_shot)
